/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.semodbus.bean;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Rainbow
 */
public class VirtualModbus
{

    /**
     *存储真实设备所对应的起始地址与结束地址
     */
    public static ArrayList<Integer> posList = new ArrayList<Integer>();

    /**
     *根据设备顺序对设备进行编号
     */
    public static HashMap<Integer,PLCDevice> devHashMap = new HashMap<Integer,PLCDevice>();
    
    /**
     *计算虚拟地址所对应的设备编号
     * @param pos
     * @return
     */
    public static int calPosToKey(int pos)
    {
        int deviceSeq = 0;
        int start = 0;
        int end =1;
        //少迭代一次，倒数第二次迭代时已经取到最后一位end值
        for (int i = 0; i < posList.size() - 1; i++)
        {
            start = posList.get(i);
            end = posList.get(i+1);
            if(start <= pos && pos <= end)
            {
                deviceSeq = i/2+1;
            }
        }
        return deviceSeq;
    }
    
    /**
     *计算设备中的真实地址
     * @param device
     * @param pos
     * @return
     */
    public static int getRealPos(PLCDevice device,int pos)
    {
        int realPos = 0;
        int offset = pos - device.getVirtualStartPos();
        realPos = device.getAddress() + offset;
        return realPos;
    }
}
